/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-19 11:33:43
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-02-18 17:38:32
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { BomChangePublicMethodServiceProxy, BomChangeServiceProxy, BomContrastiveServiceProxy, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
    selector: 'app-comparative-analysis',
    templateUrl: './comparative-analysis.component.html',
    styleUrls: ['./comparative-analysis.component.less']
})
export class ComparativeAnalysisComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    model = undefined;
    modelOptions = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    sendTime = undefined;
    sendTimeOptions = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    xj = undefined;
    xjOptions = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    bomVersion = undefined;
    bomVersionOptions = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    makeBomVersion = undefined;
    makeBomVersionOptions = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomChangePublicMethodServiceProxy: BomChangePublicMethodServiceProxy,
        private _bomChangeServiceProxy: BomChangeServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _bomContrastiveServiceProxy: BomContrastiveServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
        //获取型号下拉
        this.modelOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.modelOptions.push(data);
                }
            });
    }
    getLotNo(event) {
        //获取发次
        this.sendTimeOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.model)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.sendTimeOptions.push(data);
                }
            });
    }
    reset() {
        this.model = undefined;
        this.sendTime = undefined;
        this.xj = undefined;
        this.bomVersion = undefined;
        this.makeBomVersion = undefined;
        this.xjOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this.makeBomVersion = undefined;
    }
    getTableList(event?: LazyLoadEvent) {
        // if (this.primengTableHelper.shouldResetPaging(event)) {
        //     this.paginator.changePage(0);
        //     return;
        // }
        abp.ui.setBusy();
        this._bomContrastiveServiceProxy.getBomAnalysis(
            undefined,
            this.model,
            undefined,
            this.sendTime,
            undefined,
            this.xj,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            abp.ui.clearBusy();
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    export() {
        // if (this.saveFlag) {
        //     this.saveFlag = false;
            //导出
            this._bomContrastiveServiceProxy.getBomAnalysisToExcel(
                undefined,
                this.model,
                undefined,
                this.sendTime,
                undefined,
                this.xj,
                undefined,
                undefined,
                undefined
            ).pipe(finalize(() => { })).subscribe(result => {
                // this.saveFlag = true;
                // this._fileDownloadService.downloadTempFile(result);
                window.open(result);
                URL.revokeObjectURL(result);
            });
        // }
    }
    onChange(event) {
        if (this.model != undefined && this.sendTime != undefined) {
            //获取XJ
            this.xjOptions = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            this._commonServiceProxy.getBomXj(
                this.model,
                this.sendTime,
                undefined,
                undefined)
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    for (const item of result) {
                        let data = { label: undefined, value: undefined };
                        data.label = item.label;
                        data.value = item.label;
                        this.xjOptions.push(data);
                    }
                });
            //获取版本
            this._bomChangeServiceProxy.getVersion(
                undefined,
                this.model,
                undefined,
                this.sendTime,
                undefined
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.makeBomVersion = result;
                });
        } else {
            this.xjOptions = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            this.makeBomVersion = undefined;
        }
    }
}
